package com.example.administrator.pandanotes;

import android.content.DialogInterface;
import android.content.Intent;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import org.litepal.LitePal;
import org.litepal.crud.DataSupport;

import java.util.ArrayList;
import java.util.List;

public class BooksActivity extends AppCompatActivity {
    //变量***************************************************************************
    private List<Books> booksList = new ArrayList<>();//书籍数据
    private ListView BookListView = null;             //书籍列表
    private FloatingActionButton fAbtnAdd = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_books);
        //初始化********************************************************************
        initBooks();//初始化书籍数据
        final BooksAdapter booksAdapter = new BooksAdapter(BooksActivity.this,R.layout.book_item,booksList);
        fAbtnAdd = (FloatingActionButton)findViewById(R.id.fABtnBooksAdd);//添加书籍
        BookListView = (ListView) findViewById(R.id.LVbooks);//绑定
        BookListView.setAdapter(booksAdapter);                //设置Adapter
        //添加事件***************************************************************
        fAbtnAdd.setOnClickListener(new View.OnClickListener() {//跳转至添加
            @Override
            public void onClick(View v) {
                Intent intent =new Intent(BooksActivity.this,BooksAddActivity.class);
                startActivity(intent);
            }
        });
        BookListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Books books = booksList.get(position);          //得到点击的位置所对应的数据
                Intent intent = new Intent(BooksActivity.this,BookUpdateActivity.class);//跳到修改页面
                intent.putExtra("Book",books); //传书
                startActivity(intent);
            }
        });
        BookListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, final View view, int position, final long id) {
                /********显示对话框**********/
                AlertDialog.Builder normalDialog =
                        new AlertDialog.Builder(BooksActivity.this);
                normalDialog.setIcon(R.drawable.delete);
                normalDialog.setTitle(R.string.confirm_del);
                normalDialog.setMessage(R.string.are_your_sure);
                normalDialog.setPositiveButton(R.string.delete, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                      TextView textView = (TextView) view.findViewById(R.id.fruit_name);
                      String string = textView.getText().toString();
                      Log.d("书籍名：",string);
                      DataSupport.deleteAll(Books.class,"book_name=?",string);
                      Toast.makeText(BooksActivity.this, "已删除", Toast.LENGTH_SHORT).show();
                      initBooks();
                      booksAdapter.notifyDataSetChanged();   //更新改变数据
                    }
                });
                normalDialog.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });
                normalDialog.show();
                return true;
            }
        });
    }
    private void initBooks()
    {
        List<Books> book = DataSupport.findAll(Books.class); //得到书籍类表所有数据
        booksList.clear();
        for(Books book1:book)                                //每个数据项进行ListView的映射
        {
            Books book2 = new Books(book1.getBook_name(),R.drawable.bookclass);//实例化，赋值
            book2.setClass_name(book1.getClass_name());//别把属性丢了啊
            book2.setBook_ISBN(book1.getBook_ISBN());
            book2.setBook_author(book1.getBook_author());
            book2.setBook_press(book1.getBook_press());
            booksList.add(book2);                                               //添加
        }
    }
    //返回键调用函数，返回主页*************************************************************
    @Override
    public void onBackPressed()
    {
        Intent intent = new Intent(BooksActivity.this,MainActivity.class);
        startActivity(intent);
        this.finish(); //关闭当前Activity
    }
}
